package com.dongdong.android.demo.ui.register;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dongdong.android.demo.R;
import com.dongdong.android.framework.base.BaseActivity;
import com.dongdong.android.framework.http.resultentity.BaseRsultEntity;
import com.dongdong.android.framework.http.resultentity.UserInfoEntity;
import com.dongdong.android.framework.utils.ToastUtil;
import com.dongdong.android.framework.widget.CountdownButton;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by dongdong on 2018/4/8.
 */

public class RegisterActivity extends BaseActivity<RegisterPresenter, RegisterModel> implements RegisterContract.View {

    @BindView(R.id.top_title_tv_lift)
    TextView mTvBack;

    @BindView(R.id.top_title_tv_middle)
    TextView mTvTitle;

    @BindView(R.id.register_activity_et_username)
    EditText mEtPhone;

    @BindView(R.id.register_activity_et_password)
    EditText mEtPassWord;

    @BindView(R.id.register_activity_et_code)
    EditText mEtCode;

    @BindView(R.id.register_activity_tv_get_code)
    CountdownButton mBtnGetCode;

    @BindView(R.id.register_activity_tv_register)
    TextView mBtnRegister;

    @BindView(R.id.register_activity_tv_to_login)
    TextView mBtnToLogin;

    private String mPhone;
    private String mPassword;
    private String mCode;


    @Override
    public int getLayoutId() {
        return R.layout.register_activity;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);


    }

    @Override
    public void initData() {
        mTvTitle.setText(R.string.register_title);

    }

    @Override
    public void initListener() {
        mTvBack.setOnClickListener(this);
        mBtnGetCode.setOnClickListener(this);
        mBtnRegister.setOnClickListener(this);
        mBtnToLogin.setOnClickListener(this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
        mBtnGetCode.onDetachedFromWindow();
    }

    @Override
    public void initPresenter() {

        mPresenter.setVM(this, mModel);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_title_tv_lift:
                finish();
                break;
            case R.id.register_activity_tv_get_code:
                mPhone = mEtPhone.getText().toString().trim();
                if (TextUtils.isEmpty(mPhone)) {
                    mBtnGetCode.reStartInit();
                    ToastUtil.showLongToast(mContext, "手机号不能为空");
                } else {
                    showProgressDialog();
                    mPresenter.getCode(mPhone);
                }
                break;
            case R.id.register_activity_tv_register:
                mPhone = mEtPhone.getText().toString().trim();
                mPassword = mEtPassWord.getText().toString().trim();
                mCode = mEtCode.getText().toString().trim();
                if (TextUtils.isEmpty(mPhone)) {
                    ToastUtil.showLongToast(mContext, "手机号不能为空");
                } else if (TextUtils.isEmpty(mPassword)) {
                    ToastUtil.showLongToast(mContext, "密码不能为空");
                } else if (TextUtils.isEmpty(mCode)) {
                    ToastUtil.showLongToast(mContext, "验证码不能为空");
                } else {
                    showProgressDialog();
                    mPresenter.register();
                }
                break;
            case R.id.register_activity_tv_to_login:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onResultError(String error) {
        dismissProgressDialog();
        ToastUtil.showLongToast(this, error);

    }

    @Override
    public void onGetCodeResult(BaseRsultEntity<String> entity) {
        dismissProgressDialog();
        if (entity != null) {
            switch (entity.getCode()) {
                case "ACK":
                    break;
                case "NACK":
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onRegisterResult(BaseRsultEntity<UserInfoEntity> entity) {
        dismissProgressDialog();
        if (entity != null) {
            switch (entity.getCode()) {
                case "ACK":
                    finish();
                    break;
                case "NACK":
                    break;
                default:
                    break;
            }
        }

    }


}
